<template>
    <el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false" :visible.sync="visible"
        class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
        <el-row :gutter="15" class="">
            <el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules">
                <el-col :span="24">
                    <el-form-item label="类型" prop="type">
                        <el-select v-model="dataForm.type" placeholder="请选择" clearable required :style="{ width: '100%' }">
                            <el-option v-for="(item, index) in typeOptions" :key="index" :label="item.fullName"
                                :value="item.enCode"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="父级" prop="parentId">
                        <SHZY-TreeSelect v-model="dataForm.parentId" :defaultExpandAll="false" :options="treeData" placeholder="选择上级菜单"
                            :style="{ width: '100%' }" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="编码" prop="enCode">
                        <el-input v-model="dataForm.enCode" placeholder="请输入" clearable required :style="{ width: '100%' }"
                            maxlength="50" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="名称" prop="fullName">
                        <el-input v-model="dataForm.fullName" placeholder="请输入" clearable required
                            :style="{ width: '100%' }" maxlength="50" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="排序" prop="sortCode">
                        <el-input-number v-model="dataForm.sortCode" placeholder="数字文本" required :min="0" :step="1"
                            :style="{ width: '100%' }">
                        </el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="描述" prop="desc">
                        <el-input v-model="dataForm.desc" placeholder="请输入" type="textarea"
                            :autosize="{ minRows: 4, maxRows: 4 }" maxlength="500" clearable :style="{ width: '100%' }">
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="有效标志" prop="enabledMark">
                        <el-switch v-model="dataForm.enabledMark" :active-value="1" :inactive-value="0">
                        </el-switch>
                    </el-form-item>
                </el-col>
            </el-form>
        </el-row>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取 消</el-button>
            <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import restAPI from "@/api/common/factorystructure";
export default {
    components: {},
    props: [],
    data() {
        const validatePass = (rule, value, callback) => {
            if (!value || value.trim().length == 0) {
                this.$set(this.dataForm, rule.field, "");
                callback(new Error("不能为空"));
            } else {
                callback();
            }
        };
        return {
            btnLoading: false,
            loading: false,
            visible: false,
            treeData: [],
            dataForm: {
                id: "",
                parentId: "",
                enCode: undefined,
                fullName: undefined,
                sortCode: undefined,
                desc: undefined,
                type: undefined,
                enabledMark: 1
            },
            rules: {
                type: [
                    {
                        required: true,
                        message: "请输入类型",
                        trigger: '"change"'
                    }
                ],
                enCode: [
                    {
                        required: true,
                        message: "请输入编码",
                        validator: validatePass,
                        trigger: '"blur"'
                    }
                ],
                fullName: [
                    {
                        required: true,
                        message: "请输入名称",
                        validator: validatePass,
                        trigger: '"blur"'
                    }
                ],
                sortCode: [
                    {
                        required: true,
                        message: "请输入排序",
                        trigger: ["blur", "change"]
                    }
                ]
            },
            typeOptions: []
        };
    },
    computed: {},
    watch: {},
    created() {
        this.getDictionaryData();
    },
    mounted() { },
    methods: {
        getDictionaryData() {
            this.$store.dispatch('base/getDictionaryData', { sort: 'FactoryType' }).then((res) => {
                this.typeOptions = res
            })
        },
        goBack() {
            this.$emit("refresh");
        },
        init(id) {
            this.dataForm.id = id || 0;
            this.visible = true;
            this.$nextTick(() => {
                this.$refs["elForm"].resetFields();
                restAPI.getDictionaryTypeSelector(this.dataForm.id).then(res => {
                    this.treeData = res.data.list
                })
                if (this.dataForm.id) {
                    restAPI.GetInfo(this.dataForm).then(res => {
                        this.dataForm = res.data;

                    });
                }
            });
        },
        dataFormSubmit() {
            this.$refs["elForm"].validate(valid => {
                if (valid) {
                    if (this.dataForm.type != "Factory" && this.dataForm.parentId=="") {
                        this.$message({
                            message: "请选择父级",
                            type: "error"
                        });
                        return;
                    }
                    this.btnLoading = true;
                    const formMethod = !this.dataForm.id ? restAPI.Create : restAPI.Update;
                    formMethod(this.dataForm).then((res) => {
                        this.btnLoading = false;
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false,
                                    this.$emit('refresh', true)
                            }
                        })
                    }).catch((res) => {
                        this.btnLoading = false;
                    })
                }
            });
        }
    }
};
</script>
